package com.cn.wanxi.servlet.cuisine;

import com.alibaba.fastjson.JSON;
import com.cn.wanxi.model.CuisineModel;
import com.cn.wanxi.result.ResultModel;
import com.cn.wanxi.service.CuisineService;
import com.cn.wanxi.service.impl.CuisineServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/cuisine/findAll")
public class CuisineFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //         1.如果前端传递中文时，可以会出现乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        2.要得到前端的数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String enable=req.getParameter("enable");

        //        3.封装model
        CuisineModel cuisineModel = new CuisineModel();
        cuisineModel.setName(Tool.nullToString(name));
        cuisineModel.setPage(Tool.pageToInt(page));
        cuisineModel.setLimit(Tool.limitToInt(limit));
        cuisineModel.setEnable(Tool.stringToInt(enable));
//        4.调用服务逻辑层
        CuisineService cuisineService = new CuisineServiceImpl();
        //        5.得到统一的返回值对象
        ResultModel resultModel = cuisineService.findAll(cuisineModel);
        //       6.将数据返回给前端页面
        resp.getWriter().println(JSON.toJSONString(resultModel));
    }
}
